#include "sys.h"
bit flag200ms = 1,flag500ms = 1;
u8 time[3] = {8,30,0};
u8 RelaworkVal;

void buzzAlarm();

void main()
{
	All_Init();
	Ds1302_Init();
	Timer0Init();
	workVal = Read_24c02(50);
	Delay1ms(200);
	while(1)
	{
		if(flag200ms)
		{
			flag200ms = 0;
			RelaworkVal = Read_Pcf8591();
		}
		
		if(flag500ms)
		{
			flag500ms = 0;
			getTime(time);
		}
			
		show();
		Key_Scan();		
	}
}


void buzzAlarm()
{
		if(workVal > RelaworkVal)
		{
			buzz(1);
		}else{
			buzz(0);
		}
}
void timer0() interrupt 1
{
	static u16 T0_count1,T0_count2;
	T0_count1++;
	T0_count2++;
	
	if(T0_count1 == 200)
	{
		T0_count1 = 0;
		flag200ms = 1;
	}
	
	if(T0_count2 == 500)
	{
		T0_count2 = 0;
		flag500ms = 1;
	}
	if(workMode)
	{
			if(buzzWorkMode)
			{
				buzzAlarm();	
			}else{
				buzz(0);
			}
			if(relayMode)
				relay(1);
			else
				relay(0);
			Led_Illume(0xfd);
	}else{
			if(workVal > RelaworkVal)
				relay(1);
			else
				relay(0);	
			Led_Illume(0xfe);			
	}
	Nixie_Show();
	Nixie_Scan();	
}